/**
 * Created by zhangxiaoyu on 2016-04-09.
 */
angular.module('hyt.partnerdzService',[])
    .factory('PartnerdzListFactory',function($rootScope,ENV,$resource,Storage,$ionicLoading,$state){
        var pagesize=10;
        var caiwuListData={};
        var APIUrl= Storage.get('fwqdz');
        var resource=$resource(APIUrl);
        return{
            queryList:function(gsmc,gsdh,kssj,jssj,sid,yh_id,user_id){
                var hasNextPage =true;    //判断是否有下一页
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME:'sqlKeyBiz.page#hezuohuoban_duizhang',
                    ACTION_INFO:{
                        'pagesize':pagesize,
                        'current_page':'1',
                        'yh_id': yh_id,
                        'gsmc':gsmc,
                        'gsdh':gsdh,
                        'kssj':kssj,
                        'jssj':jssj
                    }
                },function(resp){
                    var resp=resp;
                    if(resp.ACTION_RETURN_CODE == "000000"){
                        if(resp.ACTION_INFO.RESULT_LIST.length < pagesize){
                            hasNextPage = false;
                        }
                        caiwuListData[pagesize] = {
                            hasNextPage : hasNextPage,
                            'current_page' : 2,
                            'data' : resp.ACTION_INFO.RESULT_LIST
                        };
                        $rootScope.$broadcast('PartnerdzList.partnerdzUpdated');
                    }else if (resp.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                })
            },
            //获取合作伙伴财务列表单页数据
            getCaiwu : function(){
                if(caiwuListData[pagesize] == undefined){
                    return false;
                }
                return caiwuListData[pagesize];
            },

            //获取合作伙伴财务列表更多页数据
            getMoreCaiwu:function(gsmc,gsdh,kssj,jssj,sid,yh_id,user_id){

                //为了解决一步加载的时候数据还没有加载完成  然后请求loadMore的时候  找不到数据
                if(caiwuListData[pagesize] == undefined){
                    return false;
                }
                //获取以前的数据
                var hasNextPage = caiwuListData[pagesize].hasNextPage;
                var current_page = caiwuListData[pagesize].current_page;
                var moreCydListData = caiwuListData[pagesize].data;
                //console.log(moreCydListData);
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME:'sqlKeyBiz.page#hezuohuoban_duizhang',
                    ACTION_INFO:{
                        'pagesize':pagesize,
                        'current_page':current_page,
                        'yh_id': yh_id,
                        'gsmc':gsmc,
                        'gsdh':gsdh,
                        'kssj':kssj,
                        'jssj':jssj
                    }
                },function(resp){
                    var resp=resp;
                    console.log(resp);
                    if(resp.ACTION_RETURN_CODE == "000000"){
                        if(resp.ACTION_INFO.RESULT_LIST.length < pagesize){
                            hasNextPage = false;
                        }
                        caiwuListData[pagesize] = {
                            hasNextPage : hasNextPage,
                            'current_page' : 2,
                            'data' : resp.ACTION_INFO.RESULT_LIST
                        };
                        $rootScope.$broadcast('PartnerdzList.partnerdzUpdated');
                    }else if (resp.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                })
            },
            //有下一页
            hasNextPage: function() {
                if (caiwuListData[pagesize] === undefined) {
                    return false;
                }
                return caiwuListData[pagesize].hasNextPage;
            }
        }
    })

    //合作伙伴选择
    .factory('PartnerFactory',function($resource,$rootScope,ENV,Storage,$ionicLoading,$state){
        var api =  Storage.get('fwqdz');
        var resource = $resource(api);
        var result;
        var partnerData={},
            pagesize=10;
        return{
            getPartner:function(sid,user_id,yh_id,dw_id,gsmc,gsdh){
                var hasNextPage =true;    //判断是否有下一页
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME :  'sqlKeyBiz.page#queryhzhbxx',
                    ACTION_INFO : {
                        'yh_id':yh_id,
                        'dw_id':dw_id,
                        'gsmc':gsmc,
                        'gsdh':gsdh,
                        'pagesize':'10',
                        'current_page':'1'
                    }
                },function(r){
                    if(r.ACTION_RETURN_CODE == "000000"){
                        if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                            hasNextPage = false;
                        }

                        partnerData[pagesize] = {
                            hasNextPage : hasNextPage,
                            'current_page' : 2,
                            'data' : r.ACTION_INFO.RESULT_LIST
                        };

                        $rootScope.$broadcast('PartnerList.partnerUpdated');
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                });
            },
            getpar : function(){
                if(partnerData[pagesize] == undefined){
                    return false;
                }
                return partnerData[pagesize];
            },
            getMorePar:function(sid,user_id,yh_id,dw_id,gsmc,gsdh){
                //为了解决一步加载的时候数据还没有加载完成  然后请求loadMore的时候  找不到数据
                if(partnerData[pagesize] == undefined){
                    return false;
                }

                //获取以前的数据
                var hasNextPage = partnerData[pagesize].hasNextPage;
                var current_page = partnerData[pagesize].current_page;
                var moreParListData = partnerData[pagesize].data;

                resource.save({
                    ACTION_NAME: 'sqlKeyBiz.page#queryhzhbxx',
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_INFO : {
                        'yh_id':yh_id,
                        'dw_id':dw_id,
                        'gsmc':gsmc,
                        'gsdh':gsdh,
                        'pagesize':pagesize,
                        'current_page':current_page
                    }
                }, function (r) {

                    current_page++;

                    if(r.ACTION_RETURN_CODE == "000000"){
                        if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                            hasNextPage = false;
                        }else if (r.ACTION_RETURN_CODE == "000005"){
                            $ionicLoading.show({
                                noBackdrop: true,
                                template: "您的账号已在其他设备登录",
                                duration: 1500
                            });
                            $state.go("login");
                        }

                        moreParListData = moreParListData.concat(r.ACTION_INFO.RESULT_LIST);

                        partnerData[pagesize] = {
                            hasNextPage : hasNextPage,
                            'current_page' : current_page,
                            'data' : moreParListData
                        };
                        $rootScope.$broadcast('PartnerList.partnerUpdated');
                    }

                })
            },

            hasNextPage: function() {
                if (partnerData[pagesize] === undefined) {
                    return false;
                }
                return partnerData[pagesize].hasNextPage;
            }
        }
    });

